<?php
namespace Weibo\Controller;
use Common\Controller\AdminbaseController;

/**
 * 用户关系
 * @author Baiyu 2017-01-10
 */
class AdminUserController extends AdminbaseController {


	protected $weibouser_model;
	
	public function _initialize() {
		parent::_initialize();
		$this -> weibouser_model = D('WeiboUser');
		$this -> weibouser_relation_model = D('WeiboUserRelation');
		$this -> weibouser_blacklist = D('WeiboUserBlacklist');
	}
	
	/**
	 * 用户列表-全部
	 * @author	Hongwei	2017.1.16
	 * @param	string		$status			all全部|top风云榜|verify_yes已认证|verify_no未认证|disabled已停用
	 * @param	string		$status_second		weibo_desc发布最多|follow_desc关注最多|fans_desc粉丝最多|verfiy_personal已认证-个人|verfiy_dept已认证-机构|verfiy_host已认证-主持人
	 */
	public function index($status = 'all',$status_second = 'weibo_desc'){

		$status == 'all' && $where = array('verify'=>array('NEQ',0),'status'=>1);			//全部

		//风云榜
		$status == 'top';							
		$top  = array('verfify'=>array('NEQ',0),'status'=>1);						//条件
		$status_second == 'weibo_desc' && $where = $top;						//风云榜-发布最多
		$status_second == 'follow_desc' && $where = $top;						//风云榜-关注最多
		$status_second == 'fans_desc' && $where = $top;						//风云榜-粉丝最多

		//已认证
		$status == 'verify_yes';	
		$status_second == 'verfiy_personal' && $where = array('verify'=>1,'status'=>1);		//已认证-个人
		$status_second == 'verfiy_dept' && $where = array('verify'=>2,'status'=>1);			//已认证-机构
		$status_second == 'verfiy_host' && $where = array('verify'=>3,'status'=>1);			//已认证-主持人

		//未认证
		$status == 'verify_no'  && $where = array('verify'=>0,'status'=>1);				//未认证，不分个人、机构、主持人，仅仅是一个用户而已

		$status == 'disabled' && $where = array('status'=>0);						//已停用

		//搜索
		$nickname = I('request.nickname');
		if($nickname){
			$where['nickname'] = array('like',"%$nickname%");
		}

		//分页
		$count=$this->weibouser_model->where($where)->count();
	
		//风云榜分页,最多100条数据
		if($status == 'weibo_desc' || $status == 'follow_desc' || $status == 'fans_desc' ){
			if($count > 100){$count=100;};  
		}
	
		$page = $this->page($count, 10);

		//排序 
		if($status == 'top' && $status_second == 'weibo_desc'){ 		//发布最多
			$order = 'weibo_count desc';
		}else if($status == 'top' && $status_second == 'follow_desc'){	//关注最多
			$order = 'follow_count desc';
		}else if($status == 'top' && $status_second == 'fans_desc'){		//粉丝最多
			$order = 'weibo_count desc';
		}else{
			$order = 'weibo_uid desc';
		}

		//列表
		$list = $this->weibouser_model
			->where($where)
			->order($order)
			->limit($page->firstRow, $page->listRows)
			->select();
		
		//显示
		$this -> assign('status', $status);
		$this -> assign('status_second', $status_second);
		$this -> assign("page", $page->show('Admin'));
		$this -> assign('list', $list);
		$this -> display();

	}



	//用户列表 带搜索 筛选 批量操作、查看用户 资料 发表微博数粉丝数关注数 认证情况 关注列表 粉丝列表  黑名单列表、编辑用户 资料 禁用用户 启用用户
	
	/**
	 * 查看用户详情
	 * @author	Hongwei		2017.1.19
	 * @param	number		$user_id	用户ID
	 */
	public function details($user_id = 0){
		$db_prefix=C('DB_PREFIX');
		//TODO 详情内容包括：资料、发表微博数、粉丝数、关注数、认证情况
		$user_id = I('get.user_id/d');

		//微博用户资料详情
		$userlist = $this->weibouser_model->where('user_id='.$user_id)->find();

		//我的朋友
		$myfriends_where= 'a.user_id=b.user_id and a.follow='.$user_id.' and mutual = 1';
		$myfriends_table = $db_prefix.'weibo_user_relation a,'.$db_prefix.'weibo_user b';
		$myfriends = $this->weibouser_relation_model->field('a.*,b.nickname')->table($myfriends_table)->where($myfriends_where)->order('a.rel_id desc')->select();	

		//我关注的
		$follow_other_where= 'a.follow=b.user_id and a.user_id='.$user_id.' and mutual = 0';
		$follow_other_table = $db_prefix.'weibo_user_relation a,'.$db_prefix.'weibo_user b';
		$follow_other = $this->weibouser_relation_model->field('a.*,b.nickname')->table($follow_other_table)->where($follow_other_where)->order('a.rel_id desc')->select();

		//关注我的-我的粉丝
		$follow_me_where = 'a.user_id=b.user_id and a.follow='.$user_id.' and mutual = 0';
		$follow_me_table = $db_prefix.'weibo_user_relation a,'.$db_prefix.'weibo_user b';
		$follow_me = $this->weibouser_relation_model->field('a.*,b.nickname')->table($follow_me_table)->where($follow_me_where)->order('a.rel_id desc')->select();

		//黑名单
		$black_where = 'a.target_id=b.user_id and a.user_id='.$user_id;
		$black_table = $db_prefix.'weibo_user_blacklist a,'.$db_prefix.'weibo_user b';
		$black_list = $this->weibouser_blacklist->field('a.*,b.nickname')->table($black_table)->where($black_where)->select();
		
		//赋值
		$this -> assign('userlist', $userlist);
		$this -> assign('myfriends', $myfriends);
		$this -> assign('follow_me', $follow_me);
		$this -> assign('follow_other', $follow_other);
		$this -> assign('black_list', $black_list);
		
		$this->display();
		
	}
	

	/**
	 * 关系用户列表
	 * @author	Hongwei	2017
	 * @param	number		$user_id	用户ID,查看哪个用户的好友列表
	 * @param	string		$type		列表类型 friends相互关注列表|follow关注列表|fans粉丝列表
	 */
	public function relation($user_id = 0, $type = 'friends'){
		
		//TODO 相互关注列表、关注列表、粉丝列表
		
	}
	
	/**
	 * 禁用用户
	 * @author	Hongwei		2017.1.18
	 * @param	number		$user_id	要禁用的用户ID
	 */
	public function disable($user_id = 0){

		$user_id = I('get.user_id',0,'intval');
		if (!empty($user_id)) {
			$result = $this->weibouser_model->where(array("user_id"=>$user_id))->setField('status','0');
			if ($result!==false) {
				$this->success("微博用户停用成功！");
			} else {
				$this->error('微博用户停用失败！');
			}
		} else {
			$this->error('数据传入失败！');
		}
		//TODO 禁用后推送通知用户
		
	}
	
	/**
	 * 启用用户
	 * @author	Hongwei		2017.1.18
	 * @param	number		$user_id	要启用的用户ID
	 */
	public function enable($user_id = 0){

		$user_id = I('get.user_id',0,'intval');
		if (!empty($user_id)) {
			$result = $this->weibouser_model->where(array("user_id"=>$user_id))->setField('status','1');
			if ($result!==false) {
				$this->success("微博用户启用成功！");
			} else {
				$this->error('微博用户启用失败！');
			}
		} else {
			$this->error('数据传入失败！');
		}

		//TODO 启用后推送通知用户
		
	}
}